<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="房间名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入房间名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="房间类型" prop="typeId">
        <el-select v-model="queryParams.typeId" placeholder="请选择房间类型" clearable>
          <el-option
            v-for="dict in roomTypeList"
            :key="dict.id"
            :label="dict.typeName"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="房间最小面积" prop="roomMinArea">
        <el-input
          v-model="queryParams.roomMinArea"
          placeholder="请输入房间最小面积"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="房间最大面积" prop="roomMaxArea">
        <el-input
          v-model="queryParams.roomMaxArea"
          placeholder="请输入房间最大面积"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所在楼层" prop="floorNum">
        <el-input
          v-model="queryParams.floorNum"
          placeholder="请输入所在楼层"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['shop:hotelRoom:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['shop:hotelRoom:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['shop:hotelRoom:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5" v-if="hotelEnable">
        <el-button
          type="warning"
          plain
          icon="el-icon-close"
          size="mini"
          @click="handleClose"
        >关闭</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['shop:hotelRoom:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="hotelRoomList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="缩略图" align="center" prop="imgAbbrev" width="100">
        <template v-slot="scope">
          <image-preview :src="scope.row.imgAbbrev" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="房间名称" align="center" prop="name" />
      <el-table-column label="房间类型" align="center" prop="type">
        <template v-slot="scope">
          <span v-for="item in roomTypeList" v-if="item.id === scope.row.typeId">
            {{item.typeName}}
          </span>
<!--          <dict-tag :options="dict.type.shop_hotel_room_type" :value="scope.row.type"/>-->
        </template>
      </el-table-column>
      <el-table-column label="价格（元）" align="center" prop="price" >
        <template v-slot="scope">
          <span>{{(scope.row.price / 100).toFixed(2)}}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template v-slot="scope">
          <dict-tag :options="dict.type.shop_goods_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="房间最小面积" align="center" prop="roomMinArea" />
      <el-table-column label="房间最大面积" align="center" prop="roomMaxArea" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template v-slot="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['shop:hotelRoom:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['shop:hotelRoom:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改酒店房间对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="房间描述">
          <editor v-model="form.roomDesc" :min-height="192"/>
        </el-form-item>
        <el-form-item label="房间类型，字典编码：ROOM_TYPE" prop="type">
          <el-select v-model="form.type" placeholder="请选择房间类型">
            <el-option
              v-for="dict in roomTypeList"
              :key="dict.id"
              :label="dict.typeName"
              :value="dict.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="房间最小面积" prop="roomMinArea">
          <el-input v-model="form.roomMinArea" placeholder="请输入房间最小面积" />
        </el-form-item>
        <el-form-item label="房间最大面积" prop="roomMaxArea">
          <el-input v-model="form.roomMaxArea" placeholder="请输入房间最大面积" />
        </el-form-item>
        <el-form-item label="床位描述" prop="bedDesc">
          <el-input v-model="form.bedDesc" placeholder="请输入床位描述" />
        </el-form-item>
        <el-form-item label="所在楼层" prop="floorNum">
          <el-input v-model="form.floorNum" placeholder="请输入所在楼层" />
        </el-form-item>
        <el-form-item label="有无窗户，公共字典" prop="windows">
          <el-select v-model="form.windows" placeholder="请选择有无窗户，公共字典">
            <el-option
              v-for="dict in dict.type.public_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="无线" prop="wirelessNet">
          <el-select v-model="form.wirelessNet" placeholder="请选择无线">
            <el-option
              v-for="dict in dict.type.public_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="有线宽带" prop="wiredNet">
          <el-select v-model="form.wiredNet" placeholder="请选择有线宽带">
            <el-option
              v-for="dict in dict.type.public_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否进烟，公共字典" prop="smoke">
          <el-select v-model="form.smoke" placeholder="请选择是否进烟，公共字典">
            <el-option
              v-for="dict in dict.type.public_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="房间允许入住人数" prop="personNum">
          <el-input v-model="form.personNum" placeholder="请输入房间允许入住人数" />
        </el-form-item>
        <el-form-item label="有无早餐，公共字典" prop="breakfast">
          <el-select v-model="form.breakfast" placeholder="请选择有无早餐，公共字典">
            <el-option
              v-for="dict in dict.type.public_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="食品饮品" prop="foodDesc">
          <el-input v-model="form.foodDesc" placeholder="请输入食品饮品" />
        </el-form-item>
        <el-form-item label="费用政策" prop="costDesc">
          <el-input v-model="form.costDesc" placeholder="请输入费用政策" />
        </el-form-item>
        <el-form-item label="便利设施" prop="amenityDesc">
          <el-input v-model="form.amenityDesc" placeholder="请输入便利设施" />
        </el-form-item>
        <el-form-item label="媒体科技" prop="mediumDesc">
          <el-input v-model="form.mediumDesc" placeholder="请输入媒体科技" />
        </el-form-item>
        <el-form-item label="浴室配套" prop="bathroomDesc">
          <el-input v-model="form.bathroomDesc" placeholder="请输入浴室配套" />
        </el-form-item>
        <el-form-item label="洗浴用品" prop="toiletriesDesc">
          <el-input v-model="form.toiletriesDesc" placeholder="请输入洗浴用品" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listHotelRoom, delHotelRoom, addHotelRoom, updateHotelRoom } from "@/api/shop/hotel/room";
import {listRoomType} from "@/api/shop/roomType";

export default {
  name: "HotelRoom",
  dicts: ['public_yes_no', 'shop_hotel_room_type', 'shop_goods_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 酒店id
      hotelId: null,
      // 房间类型数据
      roomTypeList: [],
      // 选中数组
      ids: [],
      // 选中数组名称
      names: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 酒店房间表格数据
      hotelRoomList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        hotelId: null,
        type: null,
        name: null,
        roomMinArea: null,
        roomMaxArea: null,
        floorNum: null,
        windows: null,
        wirelessNet: null,
        wiredNet: null,
        smoke: null,
        personNum: null,
        breakfast: null,
        foodDesc: null,
        costDesc: null,
        amenityDesc: null,
        mediumDesc: null,
        bathroomDesc: null,
        toiletriesDesc: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    const hotelId = this.$route.query && this.$route.query.hotelId;
    if (hotelId) {
      this.hotelId = hotelId;
      this.queryParams.hotelId = hotelId;
      this.getTypeList(this.hotelId);
    }
    this.getList();
  },
  activated() {
    this.getList();
  },
  computed: {
    // 是否禁用
    hotelEnable() {
      return this.hotelId;
    },
  },
  methods: {
    /** 查询酒店房间列表 */
    getList() {
      this.loading = true;
      listHotelRoom(this.queryParams).then(response => {
        this.hotelRoomList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 查询房间类型
    getTypeList(hotelId) {
      listRoomType({hotelId: hotelId}).then(response => {
        this.roomTypeList = response.rows;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        hotelId: null,
        roomDesc: null,
        type: null,
        roomMinArea: null,
        roomMaxArea: null,
        bedDesc: null,
        floorNum: null,
        windows: null,
        wirelessNet: null,
        wiredNet: null,
        smoke: null,
        personNum: null,
        breakfast: null,
        foodDesc: null,
        costDesc: null,
        amenityDesc: null,
        mediumDesc: null,
        bathroomDesc: null,
        toiletriesDesc: null,
        isDel: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.names = selection.map(item => item.name)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      // this.reset();
      // this.open = true;
      // this.title = "添加酒店房间";
      this.$tab.openPage("添加酒店房间", '/shop/hotel-room-add/index',{hotelId: this.hotelId});
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$tab.openPage("修改[" + row.name + "]房间", '/shop/hotel-room-add/index',{roomId: row.id, hotelId: this.hotelId});
      // this.reset();
      // const id = row.id || this.ids
      // getHotelRoom(id).then(response => {
      //   this.form = response.data;
      //   this.open = true;
      //   this.title = "修改酒店房间";
      // });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateHotelRoom(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addHotelRoom(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      const name = row.name || this.names;
      this.$modal.confirm('是否确认删除【' + name + '】房间？').then(function() {
        return delHotelRoom(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('shop/hotelRoom/export', {
        ...this.queryParams
      }, `hotelRoom_${new Date().getTime()}.xlsx`)
    },
    // 返回按钮
    handleClose() {
      const obj = { path: "/shop/hotel/hotel" };
      this.$tab.closeOpenPage(obj);
    },
  }
};
</script>
